﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Wpf.CommonTools.SystemExtension;

namespace Wpf.CommonTools
{
    [Serializable]
    public class EncodingContainer : IEquatable<EncodingContainer>
    {
        private string displayName;

        public string DisplayName
        {
            get { return displayName; }
            set { displayName = value; }
        }
        private Encoding encoder;

        public Encoding Encoder
        {
            get { return encoder; }
            set { encoder = value; }
        }

        #region IEquatable<EncodingContainer> 成员

        public bool Equals(EncodingContainer other)
        {
            bool b = this.displayName == other.DisplayName;
            return b;
        }

        #endregion

        public override string ToString()
        {
            return DisplayName;
        }
    }

    public class EncodingHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="firstItem"></param>
        /// <returns></returns>
        public static List<EncodingContainer> GetList(string firstItem)
        {
            List<EncodingContainer> encodingList = GetList();
            EncodingContainer first = new EncodingContainer() { DisplayName = firstItem, Encoder = null };
            encodingList.Insert(0, first);
            return encodingList;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<EncodingContainer> GetList()
        {
            List<EncodingContainer> encodingList = Encoding.GetEncodings().Select(p => new EncodingContainer() { DisplayName = p.DisplayName, Encoder = p.GetEncoding() }).OrderBy(m=>m.DisplayName).ToList();
            return encodingList;
        }
    }
}
